﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using Top.Api;
using Top.Api.Request;

namespace ALS.Component.Tools.Helpers
{
    public class TaobaoHelper
    {
        private readonly static string serverUrl;
        private readonly static string appKey;
        private readonly static string appSecret;
        private readonly static string session;
        private readonly static Lazy<ITopClient> iTopClient;
        private static ITopClient TopClient { get { return iTopClient.Value; } }

        static TaobaoHelper()
        {
            var paramSandBox = bool.Parse(ConfigurationManager.AppSettings["IsSandBox"]) ? "SandBox_" : string.Empty;
            serverUrl = ConfigurationManager.AppSettings[string.Format("{0}ServerUrl", paramSandBox)];
            appKey = ConfigurationManager.AppSettings[string.Format("{0}AppKey", paramSandBox)];
            appSecret = ConfigurationManager.AppSettings[string.Format("{0}AppSecret", paramSandBox)];
            session = ConfigurationManager.AppSettings[string.Format("{0}Session", paramSandBox)];

            iTopClient = new Lazy<ITopClient>(() => new DefaultTopClient(serverUrl, appKey, appSecret));
        }

        public static TopResponse Execute<Request>(ITopRequest<Request> request) where Request : TopResponse
        {
            return TopClient.Execute(request, session);
        }
    }
}
